package com.DMS.controller;

import com.DMS.dao.Admin;
import com.DMS.dao.Dorms;
import com.DMS.dao.Register;
import com.DMS.dao.Students;
import com.DMS.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public  class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/login")
    public String login(String username, String password) {
        Admin byUsernameAndPassword = userMapper.findByUsernameAndPassword(username, password);
        if (byUsernameAndPassword != null){
            return "登录成功";
        }
        return "登录失败";
    }

    @PostMapping("/register")
    public String register(@RequestBody Register register) {
        if(!register.getPassword().equals(register.getConfirm())){
            return "2次密码输入不一致";
        }
        Admin admin = new Admin();
        admin.setUsername(register.getUsername());
        admin.setPassword(register.getPassword());
        int res = userMapper.insertAdmin(admin);
        if(res > 0){
            return "注册成功";
        }
        return "注册失败";
    }

    @PostMapping("/changePassword")
    public String changePassword(@RequestBody Map<String,String> data) {
        Admin byUsernameAndPassword = userMapper.findByUsernameAndPassword(data.get("username"), data.get("oldPassword"));

        if(byUsernameAndPassword == null){
            return "账号或者旧密码错误（请不要留空）";
        }

        int res = userMapper.updatePassword((String) data.get("username"), (String) data.get("newPassword"));
        if(res > 0){
            return "修改成功";
        }
        return "修改失败";

    }

@PostMapping("addadmin")
public String addAdmin(@RequestBody Register register) {
    Admin admin1 = new Admin();
    admin1.setUsername(register.getUsername());
    admin1.setPassword(register.getPassword());
        int res = userMapper.insertAdmin(admin1);
        if(res > 0){
            return "注册成功";
        }
    return "注册失败";
}


    @GetMapping("/getAdmin")
    public List<Admin> getAdmin() {
        return userMapper.findAll();
    }


    @PutMapping("/updateadmin")
    public ResponseEntity<String> updateAdmin(@RequestBody Admin admin) {
        try {
            int affectedRows = userMapper.updateAdmin(admin);

            if (affectedRows > 0) {
                return ResponseEntity.ok("Update successful");
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error updating admin: " + e.getMessage());
        }
    }


    @PostMapping("/deleteadmin")
    public String deleteadmin(@RequestBody Admin admin) {
        int res=userMapper.deleteAdmin(admin.getId());
        if(res > 0){
            return "删除成功";
        }
        return "删除失败";
    }


    @GetMapping("/getstudents")
    public List<Students> getStudents() {
        return userMapper.findAllStudents();
    }

    @GetMapping("/searchStudents")
    public List<Students> searchStudents(@RequestParam String keyword) {
        return userMapper.searchStudents("%" + keyword + "%");
    }

    @PostMapping("/addstudent")
    public String addStudent(@RequestBody Students student) {
        int res = userMapper.insertStudent(student);
        if (res > 0) {
            return "新增成功";
        }
        return "新增失败";
    }

    @PutMapping("/updatestudent")
    public ResponseEntity<String> updateStudent(@RequestBody Students student) {
        try {
            int affectedRows = userMapper.updateStudent(student);

            if (affectedRows > 0) {
                return ResponseEntity.ok("Update successful");
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error updating student: " + e.getMessage());
        }
    }

    @DeleteMapping("/deletestudent/{id}")
    public String deleteStudent(@PathVariable int id) {
        int res = userMapper.deleteStudent(id);
        if (res > 0) {
            return "删除成功";
        }
        return "删除失败";
    }


    @GetMapping("/getdorms")
    public List<Dorms> getdorms() {
        return userMapper.findAllDorms();
    }

    @PostMapping("/addDorm")
    public String addDorm(@RequestBody Dorms dorm) {
        int res = userMapper.insertDorm(dorm);
        if (res > 0) {
            return "新增宿舍成功";
        }
        return "新增宿舍失败";
    }

    @PutMapping("/updateDorm")
    public ResponseEntity<String> updateDorm(@RequestBody Dorms dorm) {
        try {
            int affectedRows = userMapper.updateDorm(dorm);
            if (affectedRows > 0) {
                return ResponseEntity.ok("更新宿舍成功");
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            return ResponseEntity.status(500).body("更新宿舍失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/deleteDorm/{id}")
    public String deleteDorm(@PathVariable int id) {
        int res = userMapper.deleteDorm(id);
        if (res > 0) {
            return "删除宿舍成功";
        }
        return "删除宿舍失败";
    }

    @GetMapping("/searchDorms")
    public List<Dorms> searchDorms(@RequestParam String keyword) {
        return userMapper.searchDorms("%" + keyword + "%");
    }

    @GetMapping("/getDorm/{id}")
    public Dorms getDorm(@PathVariable int id) {
        return userMapper.getDormById(id);
    }

}


